package com.hr.剑指offer.work2.list;


/**
 * @author ：zhz
 * @date ：Created in 2020/12/08
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 两两交换链表中的节点
 **/
public class SwapPairs_24 {
    //三指针法
    public ListNode swapPairs(ListNode head) {
        ListNode cur=head;
        ListNode first=head.next;
        ListNode second=head.next.next;
        while ( cur.next!=null && cur.next.next!=null){
            first.next=cur;
            cur.next=second;
            cur=second;
            first=cur.next;
            second=first.next;
        }
        return cur;
    }
    //递归
    public ListNode swapPairs1(ListNode head){
        ListNode cur=head;
        if (cur==null || cur.next==null){
            return cur;
        }
        ListNode newHead=cur.next;
        cur.next=swapPairs1(newHead.next);
        newHead.next=cur;
        return newHead;
    }
}
